<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="cancle" :destroy-on-close="true">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="角色名称" label-width="100px" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="角色描述" label-width="100px" prop="description">
        <el-input v-model="form.description" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" @click="btn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRole, addRole, editRole, getRoleDetail } from '@/api/setting'
export default {
  props: {
    dialogVisible: {
      type: Boolean
    },
    roleDetail: {
      type: Object,
      required: true
    }
  },
  data() {
    //   验证不能有相同角色名
    const validName = async(reule, value, callback) => {
      const { rows } = await getRole()
      console.log(rows)
      console.log(this.roleDetail)
      if (this.roleDetail.id) {
        rows.some(item => item.name === value && item.name !== this.roleDetail.name) ? callback(new Error('不能重复名字')) : callback()
      } else {
        rows.some(item => item.name === value) ? callback(new Error('不能重复名字')) : callback()
      }
    }
    return {
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, trigger: 'blur', message: '不能为空' },
          { validator: validName, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 5, max: 20, trigger: 'blur', message: '5-20个字符' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.roleDetail.id ? '编辑角色' : '新增角色'
    }
  },
  methods: {
    btn() {
      this.$refs.form.validate(async valid => {
        try {
          if (valid) {
            console.log(1)
            if (this.roleDetail.id) {
              await editRole(this.roleDetail.id, this.form)
              this.$emit('loadPage')
              this.$message.success('编辑成功')
            } else {
              await addRole(this.form)
              this.$emit('loadPage')
              this.$message.success('增加成功')
            }
            this.$emit('update:dialogVisible', false)
            await this.$refs.form.resetFields()
          }
        } catch (error) {
          console.log(error)
        }
      })
    },
    cancle() {
      this.form = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    async getRoleDetail(id) {
      const res = await getRoleDetail(id)
      this.form = res
      console.log(this.form)
    }
  }
}
</script>

<style scoped>
.dialog-footer{
    display: flex;
    justify-content: center;
    padding: 0;
    margin-top: -30px;
}
</style>
